package com.th.therp.mapper;

import com.github.pagehelper.Page;
import com.th.therp.pojo.dto.page.PurchasePageQueryDTO;
import com.th.therp.pojo.emtity.Purchase;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface PurchaseMapper {

    @Insert("insert into purchase (barcode, store, vendor, material,total_amount, quantity, create_time, operator,is_deleted) VALUES (#{barcode},#{store},#{vendor},#{material},#{totalAmount},#{quantity},#{createTime},#{operator},0)")
    void add(Purchase purchase);

    Page<Purchase> page(PurchasePageQueryDTO purchasePageQueryDTO);

    // 根据时间范围查找采购订单的数量
    @Select("select sum(quantity) as quantity from purchase where create_time between #{startTime} and #{endTime} and is_deleted=0")
    Integer findQuantityByTime(String startTime, String endTime);

    // 根据时间范围查找采购订单的金额
    @Select("select sum(total_amount) as total_amount from purchase where create_time between #{startTime} and #{endTime} and is_deleted=0")
    Double findTotalAmountByTime(String startTime, String endTime);

    //根据id查询采购单
    @Select("select * from purchase where id = #{id}")
    Purchase findById(Integer id);

    //根据id删除数据(退货)
    @Delete("update purchase set is_deleted=1 where id=#{id}")
    void delete(Integer id);
}